﻿using MvvmCross.Core.ViewModels;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Sidvall.WorkItemManager.UI.ViewModels
{
    public class DocumentationViewModel : Sidvall.WorkItemManager.UI.ViewModels.Core.ViewModelBase
    {
        #region Public Members

        #region OverviewIsVisible

        private bool _OverviewIsVisible;
        public bool OverviewIsVisible
        {
            get
            {
                if (_PageType != null)
                    return (_PageType.Value == PageType.Overview);
                return _OverviewIsVisible;
            }
            set
            {
                _OverviewIsVisible = value;
                RaisePropertyChanged("OverviewIsVisible");
            }
        }

        #endregion
        #region ServerIsVisible

        private bool _ServerIsVisible;
        public bool ServerIsVisible
        {
            get
            {
                if (_PageType != null)
                    return (_PageType.Value == PageType.Server);
                return _ServerIsVisible;
            }
            set
            {
                _ServerIsVisible = value;
                RaisePropertyChanged("ServerIsVisible");
            }
        }

        #endregion
        #region WorkerIsVisible

        private bool _WorkerIsVisible;
        public bool WorkerIsVisible
        {
            get
            {
                if (_PageType != null)
                    return (_PageType.Value == PageType.Worker);
                return _WorkerIsVisible;
            }
            set
            {
                _WorkerIsVisible = value;
                RaisePropertyChanged("WorkerIsVisible");
            }
        }

        #endregion
        #region PluginIsVisible

        private bool _PluginIsVisible;
        public bool PluginIsVisible
        {
            get
            {
                if (_PageType != null)
                    return (_PageType.Value == PageType.Plugin);
                return _PluginIsVisible;
            }
            set
            {
                _PluginIsVisible = value;
                RaisePropertyChanged("PluginIsVisible");
            }
        }

        #endregion
        #region HelpTextIsVisible

        private bool _HelpTextIsVisible;
        public bool HelpTextIsVisible
        {
            get
            {
                return _HelpTextIsVisible;
            }
            set
            {
                _HelpTextIsVisible = value;
                RaisePropertyChanged("HelpTextIsVisible");
            }
        }

        #endregion
        #region ServerScript

        private string _ServerScript;
        public string ServerScript
        {
            get
            {
                return _ServerScript;
            }
            set
            {
                _ServerScript = value;
                RaisePropertyChanged("ServerScript");
            }
        }

        #endregion
        #region HelpText

        private string _HelpText;
        public string HelpText
        {
            get
            {
                return _HelpText;
            }
            set
            {
                _HelpText = value;
                RaisePropertyChanged("HelpText");
            }
        }

        #endregion
        #region PluginHtml

        private string _PluginHtml;
        public string PluginHtml
        {
            get
            {
                return _PluginHtml;
            }
            set
            {
                _PluginHtml = value;
                RaisePropertyChanged("PluginHtml");
            }
        }

        #endregion
        #region WorkerHtml

        private string _WorkerHtml;
        public string WorkerHtml
        {
            get
            {
                return _WorkerHtml;
            }
            set
            {
                _WorkerHtml = value;
                RaisePropertyChanged("WorkerHtml");
            }
        }

        #endregion

        #region SelectHelpCommand

        public ICommand SelectHelpCommand
        {
            get
            {
                return new MvxCommand<string>(SelectHelp);
            }
        }

        #endregion
        #region SelectHelp

        public void SelectHelp(string id)
        {
            if (id == _CurrentId)
                this.HelpTextIsVisible = !this.HelpTextIsVisible;
            else
                this.HelpTextIsVisible = true;
            switch (id)
            {
                case "1":
                    this.HelpText = "This is the tool you are using now";
                    break;
                case "2":
                    this.HelpText = "A database with tables that the WorkItem Manager\r\nuses to store information";
                    break;
                case "3":
                    this.HelpText = "A scheduled task with the action:\r\nSidvall.WorkItemManager.Worker.exe run [worker ids] [Plugin path]";
                    break;
                case "4":
                    this.HelpText = "Tasks assigned to the workers";
                    break;
                default:
                    break;
            }
            _CurrentId = id;
        }

        #endregion
        #region SelectPageCommand

        public ICommand SelectPageCommand
        {
            get
            {
                return new MvxCommand<string>(SelectPage);
            }
        }

        #endregion
        #region SelectPage

        public void SelectPage(string id)
        {
            this.OverviewIsVisible = false;
            this.ServerIsVisible = false;
            this.WorkerIsVisible = false;
            this.PluginIsVisible = false;
            switch (id)
            {
                case "1":
                    this.OverviewIsVisible = true;
                    break;
                case "2":
                    this.ServerIsVisible = true;
                    break;
                case "3":
                    this.WorkerIsVisible = true;
                    break;
                case "4":
                    this.PluginIsVisible = true;
                    break;
                default:
                    break;
            }
        }

        #endregion

        #region StartAsync

        protected override async Task StartAsync()
        {
            _PageType = null;
            SelectPage("1");

            this.ServerScript = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(@"Data\WorkItemManager.sql");
            this.PluginHtml = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(@"Data\Plugins.htm");
            this.WorkerHtml = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(@"Data\Worker.htm");
        }

        #endregion

        #endregion
        #region Private Members

        private string _CurrentId;
        private PageType? _PageType = PageType.Plugin;
        private enum PageType
        {
            Overview,
            Server,
            Worker,
            Plugin,
        }

        #endregion
    }
}
